/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.dialog;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import soccerscope.model.ClientScene;
import soccerscope.model.WorldModel;
import soccerscope.training.TrainingModel;
import soccerscope.training.TrainingStatsElement;

public class TrainingStatsDialog extends JDialog {

	private static final long serialVersionUID = 6167649483756338593L;

	private JPanel mainTabs;

	private TrainingModel tm;

	private TrainingStatsElement[] stats = null;

	private JLabel[] names = null;

	private JLabel[] numbers = null;

	private int numberofStats = 0;

	/**
	 * The constructor creates a reference to the scene and to the main-frame,
	 * because it is a modal dialog. From the scene, all player/ball data,
	 * including the TrainingModels are refered to.
	 * 
	 * @param frame -
	 *            the soccerscope frame
	 * @param scene -
	 *            the actual scene
	 */
	public TrainingStatsDialog(JFrame frame, ClientScene scene) {
		super(frame);

		this.setScene(scene);
		if (this.tm == null)
			System.out.println("[StatsDialog]tm ist NICHT gesetzt");

		initStatistics();
		this.tm.setTrainingStatsDialog(this);
		initGUI();
		this.pack();
	}

	/**
	 * This method sets the scene of soccerscope
	 * 
	 * @param scene -
	 *            the actual scene
	 */
	public void setScene(ClientScene scene) {
		this.tm = WorldModel.getInstance().trainModel;
	}

	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.getContentPane().setLayout(thisLayout);
			this.setResizable(false);
			this.setTitle("Dainamite Training-Statistics");
			this.mainTabs = new JPanel();
			GridBagLayout gbl = new GridBagLayout();
			GridBagConstraints gbc = new GridBagConstraints();
			this.mainTabs.setLayout(gbl);
			for (int i = 0; i < this.numberofStats; i++) {
				gbc.gridx = 0;
				gbc.gridy = i;
				gbc.ipadx = 50;
				gbc.anchor = GridBagConstraints.EAST;
				gbl.setConstraints(this.names[i], gbc);
				this.mainTabs.add(this.names[i]);
				gbc.gridx = 1;
				gbc.gridy = i;
				gbc.ipadx = 20;
				gbc.anchor = GridBagConstraints.WEST;
				gbl.setConstraints(this.numbers[i], gbc);
				this.mainTabs.add(this.numbers[i]);
			}
			this.getContentPane().add(this.mainTabs, BorderLayout.CENTER);
			this.setSize(600, 500);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setVisible(boolean flag) {
		super.setVisible(false);
	}

	private void initStatistics() {
		this.stats = this.tm.getStats();
		this.numberofStats = this.stats.length;
		this.names = new JLabel[this.numberofStats];
		this.numbers = new JLabel[this.numberofStats];
		for (int i = 0; i < this.numberofStats; i++) {
			this.names[i] = new JLabel(this.stats[i].getName());
			this.numbers[i] = new JLabel("0");
		}
	}

	public void update() {
		for (int i = 0; i < this.numberofStats; i++) {
			this.numbers[i].setText(Integer
					.toString(this.stats[i].getCounter()));
		}
		repaint();
	}
}
